import { request } from "../request";

// 返回数据接口
export interface GetQuestionsListRes {
  code: number;
  message: string;
  data: {
    questionId: number;
    creatorId: number;
    content: string;
    options: Record<string, unknown>;
    answer: string;
    difficulty: string;
    createdAt: Record<string, unknown>;
    type: number;
  }[];
  timestamp: Record<string, unknown>;
}

const token = localStorage.getItem("token");

// 获取所有题目列表
const getQuestionsListAPI = async (): Promise<GetQuestionsListRes> => {
  return request("/api/exam-questions", {
    method: "get",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      "Authorization": `Bearer ${token}`,
    },
  });
};

// 参数接口
export interface SelectQuestionsParams {
  examId: number;
  questionIds: number[];
}

// 返回数据接口
export interface SelectQuestionsRes {
  code: number;
  message: string;
  data: string;
  timestamp: Record<string, unknown>;
}

// 选择题目并关联到考试
const selectQuestionsAPI = async (params: SelectQuestionsParams): Promise<SelectQuestionsRes> => {
  const token = localStorage.getItem("token");
  return request("/api/exam-questions/select-questions", {
    method: "post",
    headers: {
      "Content-Type": "application/json",
      "Authorization": `Bearer ${token}`,
    },
    data: JSON.stringify(params),
  });
};

export { getQuestionsListAPI, selectQuestionsAPI };